package com.yanqu.road.server.cmd.activity.chuhan;

import com.yanqu.road.logic.activity.chuhan.ChuHanConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChuHanProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.ChuHanMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_CHUHAN_DEAL_EVENT, desc = "")
public class ChuHanDealEventCmd extends AbstractCommand {

    private ChuHanProto.ChuHanDealEventReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = ChuHanProto.ChuHanDealEventReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChuHanConfig config = ChuHanMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!config.inEnterActivityTime()) {
            ret = GameErrorCode.E_CHUHAN_NO_OPERATE_TIME;
            return;
        }

        ChuHanProto.CrossChuHanDealEventReqMsg.Builder builder = ChuHanProto.CrossChuHanDealEventReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setPositionNo(reqMsg.getPositionNo());
        builder.setChoice(reqMsg.getChoice());
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CHUHAN_DEAL_EVENT, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        ChuHanProto.ChuHanDealEventRespMsg.Builder respMsg = ChuHanProto.ChuHanDealEventRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.S_CHUHAN_DEAL_EVENT%20000, respMsg);
    }
}
